package com.example.demo.array;

/**
 * 移动零
 * https://leetcode.cn/problems/move-zeroes/description/
 */
public class MoveZeroes {

    public void moveZeroes(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] == 0) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] != 0) {
                        int tmp = nums[i];
                        nums[i] = nums[j];
                        nums[j] = tmp;
                        break;
                    }
                }
            }
        }
    }

    public void moveZeroes2(int[] nums) {
        int len = nums.length;
        int preIndex = 0;
        int curIndex = 0;

        while (curIndex < len) {
            if (nums[curIndex] != 0) {
                nums[preIndex++] = nums[curIndex];
            }
            curIndex++;
        }

        while (preIndex < len) {
            nums[preIndex] = 0;
            preIndex++;
        }
    }
}
